package com.huiyeji.dynamic.core.bean;

import com.huiyeji.dynamic.core.DynamicContext;
import com.huiyeji.dynamic.core.annotations.*;
import com.huiyeji.dynamic.core.exception.DynamicForcedTerminationException;

/**
 * @author Huiyeji
 */
@DynamicBean("program")
public class DynamicProgram {

    private final DynamicContext context;

    public DynamicProgram(DynamicContext context){
        this.context = context;
    }

    @DynamicMethod("if")
    public DynamicCondition IF(Boolean condition){
        return new DynamicCondition(context, condition);
    }

    @DynamicMethod("if")
    public DynamicCondition IF(){
        return new DynamicCondition(context);
    }

    @DynamicMethod("for")
    public DynamicFor FOR(Object object,String objectName){
        return new DynamicFor(context,object,objectName);
    }

    @DynamicMethod("end")
    public void END(){
        context.getPointer().setNextPosition(context.getPointer().getSize());
    }

    @DynamicMethod("error")
    public void ERROR(){
        throw new DynamicForcedTerminationException();
    }

    @DynamicMethod("error")
    public void ERROR(String message){
        throw new DynamicForcedTerminationException(message);
    }

    @DynamicMethod("print")
    public void print(Object object){
        System.out.println(object == null ? null : object.toString());
    }

    @DynamicMethod("loop")
    public DynamicLoop loop(){
        return new DynamicLoop(context);
    }

    @DynamicMethod("sleep")
    public void sleep(int seconds) throws InterruptedException {
        Thread.sleep(seconds * 1000L);
    }

    @DynamicMethod("timestamp")
    public long timestamp(){
        return System.currentTimeMillis();
    }
}
